package cn.itcast.bos.web.action.base;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 收派标准提交到服务器
 *
 * @author 朱志强
 * @version 1.0 2017年9月3日下午6:42:11
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class StandardAction extends ActionSupport implements ModelDriven<Standard> {
	
	/**
	 * 序列化
	 */
	private static final long serialVersionUID = 8040332957997598885L;
	
	/** 模型驱动 */
	private Standard standard = new Standard();

	@Override
	public Standard getModel() {
		return standard;
	}
	
	/**
	 * 注入service
	 */
	@Autowired
	private StandardService standardService;
	
	/**
	 * 添加操作
	 * @return
	 */
	@Action(value="standard_save",results={@Result(name="success",type="redirect",location="./pages/base/standard.html")})
	public String save(){
		System.out.println("添加收派标准....");
		standardService.save(standard);
		return SUCCESS;
	}
	
	//属性驱动,分页查询,根据浏览器发送的请求数据,进行数据接收
	/** 当前页数*/
	private int page;
	/** 当前页显示条数 */
	private int rows;
	
	public void setPage(int page) {
		this.page = page;
	}
	
	public void setRows(int rows) {
		this.rows = rows;
	}
	
	@Action(value="standard_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		//调用业务层,查询数据结果
		Pageable pageable = new PageRequest(page-1, rows);
		Page<Standard> pageData = standardService.findPageData(pageable);
		
		//响应客户端 ,数据返回解析需要rows和total
		Map<String,Object> result = new HashMap<String, Object>();
		result.put("total",pageData.getNumberOfElements());
		result.put("rows",pageData.getContent());
		
		//将map转换为json数据返回,使用struts2-json-plugin插件
		ActionContext.getContext().getValueStack().push(result);
		
		return SUCCESS;
		
	}
	
	
}
